<?php
namespace Core\Forms\Element;

use Core\Forms\Element;
use Phalcon\Forms\Exception;
use Phalcon\Tag\Select as TagSelect;

class Select extends Element
{
    protected $_optionsValues;

    public function __construct($name, $options = null, $attributes = null)
    {
        if (is_object($options) === false &&
            is_array($options) === false &&
            is_null($options) === false) {
            throw new Exception('Invalid parameter type.');
        }

        $this->_optionsValues = $options;
        parent::__construct($name, $attributes);
    }

    public function setOptions($options)
    {
        if (is_object($options) === false &&
            is_array($options) === false) {
            throw new Exception('Invalid parameter type.');
        }

        $this->_optionsValues = $options;

        return $this;
    }

    public function getOptions()
    {
        return $this->_optionsValues;
    }

    public function addOption($option)
    {
        if (is_array($option) === false) {
            throw new Exception('Invalid parameter type.');
        }

        $this->_optionsValues[] = $option;

        return $this;
    }

    public function render($attributes = null)
    {
        if (is_array($attributes) === false &&
            is_null($attributes) === false) {
            throw new Exception('Invalid parameter type.');
        }

        //Merge passed attributes with previously defined ones
        return TagSelect::selectField(
            $this->prepareAttributes($attributes),
            $this->_optionsValues
        );
    }
}
